import { Component, ViewChild } from '@angular/core';
import { ICellRendererAngularComp} from '@ag-grid-community/angular';
import { ApesCheckboxComponent } from '@shared/apes/checkbox';
@Component({
  selector: 'apes-cell-check',
  template: `
    <label #checkBox apes-checkbox  [(ngModel)]="inputValue" (ngModelChange)="onChange($event)" [apesDisabled]="disabled"></label>
     `,
  styles: []
})
export class EditorCheckBoxComponent implements ICellRendererAngularComp {
  inputValue: any;
  params: any;
  ctx: any;
  disabled = false;

  @ViewChild('checkBox', {read: ApesCheckboxComponent,static:false}) checkBox: ApesCheckboxComponent;

  constructor() {
  }

  agInit(params: any): void {
    if (params.hasOwnProperty("colDef")){
      this.disabled = params.colDef.editable==true? false:true;
    }else{
      this.disabled = params.column.colDef.editable==true? false:true;
    }

    this.params = params;
    this.inputValue = params.value;
  }
  ngAfterViewInit() {
    setTimeout(() => {
      this.checkBox.focus();
    }, 500);
  }

  onChange(param){
    this.params.value = param;
    this.params.node.setDataValue(this.params.colDef, this.inputValue ? true : false);
  }

  refresh(params: any): boolean {
    params.data[this.params.colDef] = params.value;
    params.api.refreshCells(params);
    return false;
  }
}
